<template>
  <div>
    <a-card :bordered="false">
      <f-search-form :more="false" @searchHandler="searchHandler">
        <f-search-form-item label="标题" name="topicTitle"/>
        <f-search-form-item label="类型" name="topicType" type="select" :data="api.topic.topicType"/>
      </f-search-form>
      <f-table ref="table" size="default" rowKey="topicId" :scroll="scroll"  :columns="columns" handler="shopTopicListHandler" :multiple="false" :operator="operator" :params="queryParam" :api="api.topic">
          <span slot="coverImage" slot-scope="text, record, index">
          <img v-lazy="record.coverImage" v-viewer class="radius-img">
        </span>
        <span slot="state" slot-scope="text, record, index">
          <a-switch v-model="record.state==1" checkedChildren="已发布" unCheckedChildren="未发布" @change="(checked) => setState(checked,record)"/>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="$refs.topicProductDrawer.handleShow(record)">活动商品</a>
            <a-divider type="vertical"/>
             <a @click="$refs.modal.handleShow(record)">编辑</a>
            <a-divider type="vertical"/>
          </template>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item v-if="$auth('table.delete')">
                <a href="javascript:;" @click="$refs.table.remove(record.topicId)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </f-table>
      <topic-drawer ref="modal"></topic-drawer>
      <topic-product-drawer ref="topicProductDrawer"></topic-product-drawer>
    </a-card>
  </div>
</template>

<script>

  import TopicDrawer from './topic_drawer'
  import TopicProductDrawer from './product/topic_product_drawer'


  export default {
    name: 'TableList',
    components: {
      TopicProductDrawer,
      TopicDrawer
    },
    data() {
      let me = this
      return {
        //列表初始化查询参数
        queryParam: {},
        scroll: { x: 1350 },
        // 表头
        columns: [
          {
            title: '#',
            align: 'center',
            customRender: (text, record, index) => index + 1
          }, {
            title: '封面图',
            align: 'center',
            scopedSlots: { customRender: 'coverImage' }
          },
          {
            title: '标题',
            dataIndex: 'topicTitle'
          },
          {
            title: '活动类型',
            dataIndex: 'topicType',
            customRender: function(text) {
              return me.columnFormatter(me.api.topic.topicType, text)
            }
          },
          {
            title: '开始日期',
            dataIndex: 'beginDate',
            customRender: function(text) {
              return me.$moment(text).format('YYYY-MM-DD')
            }
          },
          {
            title: '结束日期',
            dataIndex: 'endDate',
            customRender: function(text) {
              return me.$moment(text).format('YYYY-MM-DD')
            }
          },
          {
            title: '商品数量',
            dataIndex: 'productNum'
          },
          {
            title: '发布状态',
            align: 'center',
            dataIndex: 'state',
            scopedSlots: { customRender: 'state' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            fixed: 'right',
            scopedSlots: { customRender: 'action' }
          }
        ],
        //操作按钮
        operator: [
          {
            name: '添加',
            icon: 'plus',
            that: this,
            handler: function(table, that) {
              that.$refs.modal.handleShow()
            }
          },
          {
            name: '删除',
            icon: 'delete',
            batch: true,
            handler: function(table) {
              table.remove()
            }
          }
        ]
      }
    },
    created() {

    },
    methods: {
      setState(check, record) {
        this.http.post(this, {
          url: this.api.topic.setState,
          data: {
            ids: record.topicId,
            state: Number(check)
          },
          success(me, data) {
            record.state = data
          }
        })
      }
    }
  }
</script>
